package com.sakura.thread.chapter1.test11;

/**
 * Created by apple on 2018/12/11.
 */
public class ThreadLocalTest {
    static ThreadLocal<String> localVariable = new ThreadLocal<>();
    static void print(String str){
        System.out.println(str+":"+localVariable.get());

        //localVariable.remove();
    }

    public static void main(String[] args) {
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                localVariable.set("threadOne local variable");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                print("threadOne");

                System.out.println("threadOne remove after "+":"+localVariable.get());
            }
        });

        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                localVariable.set("threadTwo local variable");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                print("threadTwo");

                System.out.println("threadTwo remove after "+":"+localVariable.get());
            }
        });

        threadOne.start();
        threadTwo.start();
    }
}
